/**
 *  Copyright � 2010 Digia Plc
 *  Copyright � 2010 Nokia Corporation
 *
 *  All rights reserved.
 *
 *  Nokia and Nokia Connecting People are registered trademarks of
 *  Nokia Corporation.
 *  Java and all Java-based marks are trademarks or registered
 *  trademarks of
 *  Sun Microsystems, Inc. Other product and company names
 *  mentioned herein may be
 *  trademarks or trade names of their respective owners.
 *
 *
 *  Subject to the conditions below, you may, without charge:
 *
 *  �  Use, copy, modify and/or merge copies of this software and
 *     associated documentation files (the "Software")
 *
 *  �  Publish, distribute, sub-licence and/or sell new software
 *     derived from or incorporating the Software.
 *
 *
 *  This file, unmodified, shall be included with all copies or
 *  substantial portions
 *  of the Software that are distributed in source code form.
 *
 *  The Software cannot constitute the primary value of any new
 *  software derived
 *  from or incorporating the Software.
 *
 *  Any person dealing with the Software shall not misrepresent
 *  the source of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 *  KIND, EXPRESS OR IMPLIED,
 *  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *  CONNECTION WITH THE
 *  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import Qt 4.7
import "javascript/sql.js" as HCS
import "../colibri"

Rectangle {
    id: structure

    property string room : "room1"
    property bool lights : false
    property int lightVal : 0
    property int volume : 0
    property bool mute : true
    property bool fireAlarm : false
    property bool burglarAlarm : false
    property int amountNum : countAmount()
    property int iconSize : 28
    property bool doReload : true;

    /**
     * Counts the icons currently visible
     *
     * @return int
     */
    function countAmount() {
        var numValue = 2;
        if( fireAlarm ) numValue = numValue + 1;
        if( burglarAlarm ) numValue = numValue + 1;
        structure.width = ( numValue < 2 ) ? (1*iconSize + 2*gridi.spacing) : (2*iconSize + 3*gridi.spacing);
        structure.height = ( numValue < 3 ) ? (1*iconSize + 2*gridi.spacing) : (2*iconSize + 3*gridi.spacing);
        return numValue;
    }

    /**
     * Checks the info from database
     *
     * @return Nothing
     */
    function checkDetails() {
        HCS.getRoom(structure.room);
        structure.lights = ( HCS.roomx[0] == 0 ) ? false : true;
        structure.lightVal = HCS.roomx[0];
        structure.mute = HCS.roomx[2];
        structure.volume = (structure.mute)?0:HCS.roomx[1];
        structure.fireAlarm = HCS.roomx[3];
        structure.burglarAlarm = HCS.roomx[4];
        amountNum = countAmount();
        return;
    }

    /**
     * Sets values for histogram (speaker volume strength)
     *
     * @param value The current volume level
     * @return Nothing
     */
    function histogramValues(value){
        var array = new Array(3)
        if ( 0 <= value && value < 100/3){
            array[0]=new Array(2*value/(100/3),"1")
            array[1]=new Array(3*value/(100/3),"1")
            array[2]=new Array(4*value/(100/3),"1")
        }
        if (100/3 <= value && value < 100/1.5){
            array[0]=new Array(3*value/(100/1.5),"1")
            array[1]=new Array(5*value/(100/1.5),"1")
            array[2]=new Array(7*value/(100/1.5),"1")
        }
        if (100/1.5 <= value && value <= 100){
            array[0]=new Array(4*value/(100),"1")
            array[1]=new Array(7*value/(100),"1")
            array[2]=new Array(10*value/(100),"1")
        }
        histogram.setValues(array);
    }

    onRoomChanged: if( room == "sauna" ) room = "kph";
    onVolumeChanged: histogramValues( volume );

    color: "transparent"

    Timer {
        interval: 1000; running: structure.doReload; repeat: true;
        triggeredOnStart: true;
        onTriggered: checkDetails();
    }

    Grid {
        id: gridi
        anchors.centerIn: parent
        columns: 2
        spacing: 3

        Item {
            width: iconSize
            height: iconSize

            Image {
                source: "images/lamp_off_30x30.png"
                width: iconSize
                height: iconSize
                smooth: true
            }

            Image {
                source: "images/lamp_on_30x30.png"
                width: iconSize
                height: iconSize
                smooth: true
                opacity: structure.lightVal / 100
            }
        }

        Item {
            width: iconSize
            height: iconSize

            Image {
                source: "images/speaker_mute_50x50.png"
                visible: ( volume == 0 )?true:false
                width: iconSize
                height: iconSize
                smooth: true
            }

            Image {
                source: "images/speaker_30x30.png"
                visible: ( volume > 0 )?true:false
                width: iconSize
                height: iconSize
                smooth: true
            }

            CLHistogram{
                id: histogram
                gradientWhenDefault:Gradient {
                    GradientStop { position: 0.0; color: "#8aff00" }
                    GradientStop { position: 1.0; color: "#57b229" }
                }
                colorWhenDefault: "#8aff00"
                maxValue: 25
                anchors.right : parent.right
                anchors.bottom: parent.bottom
                width: parent.width
                height: parent.height*1.3
                spacing: 2
                visible: ( volume > 0 )?true:false
            }
        }

        Image {
            source: "images/fire_trans_30x30_png.png"
            visible: ( fireAlarm )?true:false
            width: iconSize
            height: iconSize
            smooth: true
        }

        Image {
            source: "images/alarm_40x40.png"
            visible: ( burglarAlarm )?true:false
            width: iconSize
            height: iconSize
            smooth: true
        }
    }
}
